package com.ygs.secs.controller;

import com.ygs.secs.model.Admin;
import com.ygs.secs.model.Instructor;
import com.ygs.secs.service.InstructorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class InstructController {
    @Autowired
    private InstructorService instructorService;

    @RequestMapping("/getInstructById")
    @ResponseBody
    public Instructor getInstructById(String instructorid) {
        Instructor instructor = instructorService.selectByPrimaryKey(instructorid);
        return instructor;
    }

    /**
     * 管理员登录
     *
     * @param adminid  账号
     * @param password 密码
     * @return 通用json返回格式
     */
    @RequestMapping("/instructorlogin")
    @ResponseBody
    public Map<String, Object> instructorlogin(String adminid, String password) {
        Map<String, Object> map = new HashMap<>();
        try {
            Instructor instructor = instructorService.selectByPrimaryKey(adminid);
            if (instructor != null) {
                if (instructor.getPassword().equals(password)) {
                    map.put("code", 200);
                    map.put("msg", "登录成功");
                    map.put("data", instructor);
                    map.put("admintype", 202);
                } else {
                    map.put("code", 201);
                    map.put("msg", "密码错误");
                    map.put("data", null);
                }
            } else {
                map.put("code", 500);
                map.put("msg", "账户不存在，请联系相关工作人员");
                map.put("data", null);
            }

        } catch (Exception e) {
            map.put("code", 500);
            map.put("msg", "system error");
            map.put("data", null);
        }
        return map;
    }

    /**
     * 查询instructor账户信息
     *
     * @param instructorid 辅导员ID
     * @return 通用json返回格式
     */
    @RequestMapping("/getinstructorByid")
    @ResponseBody
    public Map<String, Object> getAdminData(String instructorid) {
        Map<String, Object> map = new HashMap<>();
        try {
            Instructor instructor = instructorService.selectByPrimaryKey(instructorid);
            if (instructor != null) {
                map.put("code", 200);
                map.put("msg", "数据查询成功");
                map.put("data", instructor);
            } else {
                map.put("code", 201);
                map.put("msg", "数据查询失败，没有此账户");
                map.put("data", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("code", 500);
            map.put("msg", "system error ");
        }
        return map;
    }

    /**
     * 修改admin账户信息
     *
     * @param instructorid   辅导员ID
     * @param counselorsname 辅导员姓名
     * @return 通用json返回格式
     */
    @RequestMapping("/updateInstructor")
    @ResponseBody
    public Map<String, Object> updateInstructor(String instructorid, String counselorsname) {
        Map<String, Object> map = new HashMap<>();
        Instructor instructor = instructorService.selectByPrimaryKey(instructorid);
        if (instructor.getInstructorid().equals(instructorid)) {
            instructor.setCounselorsname(counselorsname);
            try {
                int i = instructorService.updateByPrimaryKey(instructor);
                if (i > 0) {
                    map.put("code", 200);
                    map.put("msg", "资料修改成功");
                    map.put("data", instructor);
                }
            } catch (Exception e) {
                e.printStackTrace();
                map.put("code", 500);
                map.put("msg", "系统错误");
            }
        } else {
            map.put("code", 201);
            map.put("msg", "修改失败，请重试！");
        }
        return map;
    }

    /**
     * 修改admin 密码
     *
     * @param instructorid 辅导员id
     * @return 通用json返回格式
     */
    @RequestMapping("/updateinstructorPwd")
    @ResponseBody
    public Map<String, Object> updateAdminPwd(String instructorid, String oldPwd, String newPwd) {
        Map<String, Object> map = new HashMap<>();
        Instructor instructor = instructorService.selectByPrimaryKey(instructorid);
        if (instructor.getPassword().equals(oldPwd)) {
            instructor.setPassword(newPwd);
            try {
                int i = instructorService.updateByPrimaryKey(instructor);
                if (i > 0) {
                    map.put("code", 200);
                    map.put("msg", "密码修改成功");
                    map.put("data", instructor);
                }
            } catch (Exception e) {
                e.printStackTrace();
                map.put("code", 500);
                map.put("msg", "系统错误");
            }
        } else {
            map.put("code", 201);
            map.put("msg", "修改失败，请重试！");
        }
        return map;
    }

}
